//
//  NSArray+KCAdd.m
//  KCKit
//
//  Created by king on 16/2/27.
//  Copyright © 2016年 KC. All rights reserved.
//

#import "NSArray+KCAdd.h"

#import "KCKitMacro.h"

#import "NSData+KCAdd.h"

KCSYNTH_DUMMY_CLASS(NSArray_KCAdd)

@implementation NSArray (KCAdd)

+ (NSArray *)arrayWithPlistData:(NSData *)plist{
    
    if (!plist) {
        return nil;
    }
    NSArray *array = [NSPropertyListSerialization propertyListWithData:plist options:NSPropertyListImmutable format:NULL error:NULL];
    if ([array isKindOfClass:[NSArray class]]) {
        return array;
    }
    return nil;
    
}
+ (NSArray *)arrayWithPlistString:(NSString *)plist{
    
    if (!plist) {
        return nil;
    }
    NSData *data = [plist dataUsingEncoding:NSUTF8StringEncoding];
    return [self arrayWithPlistData:data];
}
- (NSData *)plistData{
    
    return [NSPropertyListSerialization dataWithPropertyList:self format:NSPropertyListBinaryFormat_v1_0 options:kNilOptions error:NULL];
    
}
- (NSString *)plistString{
    
    NSData *xmlData = [NSPropertyListSerialization dataWithPropertyList:self format:NSPropertyListBinaryFormat_v1_0 options:kNilOptions error:NULL];
    if (xmlData) {
        return xmlData.utf8String;
    }
    return nil;
    
}
- (id)randomObject{
    
    if (self.count) {
        return self[arc4random_uniform((u_int32_t)self.count)];
    }
    return nil;
}
- (id)objectOrNilAtIndex:(NSUInteger)index{
    return index < self.count ? self[index] : nil;
}

- (NSString *)jsonStringEncoded{
    
    if ([NSJSONSerialization isValidJSONObject:self]) {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:0 error:&error];
        NSString *json = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        return json;
    }
    return nil;
}
- (NSString *)jsonPrettyStringEncoded {
    if ([NSJSONSerialization isValidJSONObject:self]) {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
        NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        return json;
    }
    return nil;
}
















@end




@implementation NSMutableArray (YYAdd)

+ (NSMutableArray *)arrayWithPlistData:(NSData *)plist{
    
    if (!plist) {
        return nil;
    }
    NSMutableArray *array = [NSPropertyListSerialization propertyListWithData:plist options:NSPropertyListMutableContainersAndLeaves format:NULL error:NULL];
    if ([array isKindOfClass:[NSMutableArray class]]) {
        return array;
    }
    return nil;
    
}

+ (NSMutableArray *)arrayWithPlistString:(NSString *)plist{
    
    if (!plist) {
        return nil;
    }
    NSData *data = [plist dataUsingEncoding:NSUTF8StringEncoding];
    return [self arrayWithPlistData:data];
    
}
- (id)randomObject{
    
    if (self.count) {
        return self[arc4random_uniform((u_int32_t)self.count)];
    }
    return nil;
}
- (id)objectOrNilAtIndex:(NSUInteger)index{
    
    return index < self.count ? self[index] : nil;
    
}
- (void)removeObjectOrNilAtIndex:(NSUInteger)index{
    
    if (index < self.count) {
        [self removeObjectAtIndex:index];
    }

}

- (void)removeFirstObject{
    
    if (self.count) {
        [self removeObjectOrNilAtIndex:0];
    }
    
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
-(void)removeLastObject{
    
    if (self.count) {
        [self removeObjectOrNilAtIndex:self.count - 1];
    }
    
}

#pragma clang diagnostic pop
- (id)popFirstObject{
    
    id obj = nil;
    if (self.count) {
        obj = self.firstObject;
        [self removeFirstObject];
    }
    return obj;
    
}
- (id)popLastObject{
    
    id obj = nil;
    if (self.count) {
        obj = self.lastObject;
        [self removeLastObject];
    }
    return obj;
    
}
- (void)appendObject:(id)anObject{
    
    [self addObject:anObject];
    
}
- (void)prependObject:(id)anObject{
    
    [self insertObject:anObject atIndex:0];

}
- (void)appendObjects:(NSArray *)objects{
    
    if (!objects) {
        return;
    }
    [self addObjectsFromArray:objects];
    
}
- (void)prependObjects:(NSArray *)objects{
    
    if (!objects) {
        return;
    }
    NSUInteger i = 0;
    for (id obj in objects) {
        [self insertObject:obj atIndex:i++];
    }
    
}
- (void)insertObjects:(NSArray *)objects atIndex:(NSUInteger)index{
    
    NSUInteger i = index;
    for (id obj in objects) {
        [self insertObject:obj atIndex:i++];
    }
    
}
- (void)reverse{
    
    NSUInteger count = self.count;
    int mid = floor(count/2);
    for (NSUInteger i = 0; i < mid; i++) {
        [self exchangeObjectAtIndex:i withObjectAtIndex:(count - (i+1))];
    }
    
}
- (void)shuffle{
    
    for (NSUInteger i = self.count; i > 1; i--) {
        
        [self exchangeObjectAtIndex:(i-1) withObjectAtIndex:arc4random_uniform((u_int32_t)i)];
        
    }
    
}























@end